package com.EAsystem.service.Impl;

import com.EAsystem.entity.Homework;
import com.EAsystem.mapper.HomeworkMapper;
import com.EAsystem.service.HomeworkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 作业管理Service实现类
 */
@Service
public class HomeworkServiceImpl implements HomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;

    @Override
    public boolean addHomework(Homework homework) {
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        homework.setCreateTime(now);
        homework.setUpdateTime(now);
        return homeworkMapper.insertHomework(homework) > 0;
    }

    @Override
    public boolean updateHomework(Homework homework) {
        // 设置更新时间
        homework.setUpdateTime(LocalDateTime.now());
        return homeworkMapper.updateHomework(homework) > 0;
    }

    @Override
    public boolean deleteHomework(Long id) {
        try {
            return homeworkMapper.deleteHomeworkById(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Homework getHomeworkById(Long id) {
        try {
            return homeworkMapper.findHomeworkById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    @Override
    public List<Homework> getHomeworksByCourseId(Long courseId) {
        try {
            return homeworkMapper.findHomeworksByCourseId(courseId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }



    @Override
    public PageInfo<Homework> getHomeworksByConditionWithPage(Homework homework, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Homework> homeworks = homeworkMapper.findHomeworksByCondition(homework);
        return new PageInfo<>(homeworks);
    }
}